package com.sunway.netty.firstexample;

import java.net.URI;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.CharsetUtil;

/**
 * 自定义的Http请求处理类
 *
 */
public class TestHttpServerHandler extends SimpleChannelInboundHandler<HttpObject> {

	/**
	 * 读取客户端发送的请求，并向客户端返回对应的响应
	 */
	@Override
	protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
		if (msg instanceof HttpRequest) {
			System.out.println("执行channelRead0");
			HttpRequest request = (HttpRequest)msg;
			System.out.println("请求方法名：" + request.method().name());
			URI uri = new URI(request.uri());
			if("/favicon.ico".equals(uri.getPath())) {
				System.out.println("请求图标资源：不返回结果");
				return;
			}
			else {
				System.out.println("请求路径：" + uri.getPath());
				ByteBuf content = Unpooled.copiedBuffer("Hello world", CharsetUtil.UTF_8);
				FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
						content);
				response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain").set(HttpHeaderNames.CONTENT_LENGTH,
						content.readableBytes());
				ctx.writeAndFlush(response);
			}
			
		}
	}

	@Override
	public boolean acceptInboundMessage(Object msg) throws Exception {
		System.out.println("执行acceptInboundMessage方法");
		return super.acceptInboundMessage(msg);
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		System.out.println("执行channelRead方法");
		super.channelRead(ctx, msg);
	}

	@Override
	public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
		System.out.println("执行channelRegistered方法");
		super.channelRegistered(ctx);
	}

	@Override
	public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
		System.out.println("执行channelUnregistered方法");
		super.channelUnregistered(ctx);
	}

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		System.out.println("执行channelActive方法");
		super.channelActive(ctx);
	}

	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		System.out.println("执行channelInactive方法");
		super.channelInactive(ctx);
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		System.out.println("执行channelReadComplete方法");
		super.channelReadComplete(ctx);
	}

	@Override
	public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
		System.out.println("执行channelWritabilityChanged方法");
		super.channelWritabilityChanged(ctx);
	}

	@Override
	public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
		System.out.println("执行handlerAdded方法");
		super.handlerAdded(ctx);
	}

	@Override
	public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
		System.out.println("执行handlerRemoved方法");
		super.handlerRemoved(ctx);
	}

	
	
}
